package github.kawaiior.yuanchuxiuzhen.common.container;

import github.kawaiior.yuanchuxiuzhen.common.container.number.PelletFurnaceContainerItemNumber;
import github.kawaiior.yuanchuxiuzhen.common.container.number.WeaponFurnaceContainerItemNumber;
import github.kawaiior.yuanchuxiuzhen.common.tileentity.PelletFurnaceTileEntity;
import github.kawaiior.yuanchuxiuzhen.common.tileentity.WeaponFurnaceTileEntity;
import github.kawaiior.yuanchuxiuzhen.core.init.BlockInit;
import github.kawaiior.yuanchuxiuzhen.core.init.ContainerInit;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.inventory.container.ClickType;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.ContainerType;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IWorldPosCallable;
import net.minecraft.util.math.BlockPos;

import javax.annotation.Nullable;
import java.util.Objects;

public class WeaponFurnaceContainer extends Container {

    public final WeaponFurnaceTileEntity tileEntity;
    private final IWorldPosCallable ableToInteract;

    private WeaponFurnaceContainerItemNumber intArray;

    public WeaponFurnaceContainer(final int id, final PlayerInventory playerInventory, final WeaponFurnaceTileEntity entity, WeaponFurnaceContainerItemNumber intArray) {
        super(ContainerInit.WEAPON_FURNACE_CONTAINER.get(), id);
        this.tileEntity = entity;
        this.ableToInteract = IWorldPosCallable.of(Objects.requireNonNull(tileEntity.getWorld()), tileEntity.getPos());

        this.intArray = intArray;

        Inventory miscInventory = entity.getMiscInventory();

        this.addSlot(new Slot(miscInventory, 0, 25, 12));
        this.addSlot(new Slot(miscInventory, 1, 43, 30));
        this.addSlot(new Slot(miscInventory, 2, 25, 48));
        this.addSlot(new Slot(miscInventory, 3, 7, 30));

        this.addSlot(new Slot(entity.getPelletInventory(), 0, 25, 30));

        this.addSlot(new Slot(entity.getFireInventory(), 0, 70, 48));

        this.addSlot(new Slot(entity.getWeaponInventory(), 0, 133, 30));
        this.addSlot(new Slot(entity.getWeaponInventory(), 1, 151, 30));
        this.addSlot(new Slot(entity.getWeaponInventory(), 2, 133, 12));
        this.addSlot(new Slot(entity.getWeaponInventory(), 3, 151, 12));

        this.addSlot(new Slot(entity.getJadeInventory(), 0, 70, 7));

        // add player inventory
        for (int i = 0; i < 9; i++) {
            this.addSlot(new Slot(playerInventory, i, i * 18 + 8, 152 - 10));
            this.addSlot(new Slot(playerInventory, i + 9, i * 18 + 8, 94 - 10));
            this.addSlot(new Slot(playerInventory, i + 18, i * 18 + 8, 112 - 10));
            this.addSlot(new Slot(playerInventory, i + 27, i * 18 + 8, 130 - 10));
        }
    }

    public WeaponFurnaceContainer(int id, PlayerInventory playerInventory, PacketBuffer data, WeaponFurnaceContainerItemNumber intArray) {
        this(id, playerInventory, getTileEntity(playerInventory, data), intArray);
    }

    public static WeaponFurnaceTileEntity getTileEntity(PlayerInventory playerInventory, PacketBuffer data) {
        Objects.requireNonNull(playerInventory, "玩家物品栏不能为空");
        Objects.requireNonNull(data, "数据包不能为空");

        BlockPos blockPos = data.readBlockPos();
        TileEntity tileEntity = playerInventory.player.world.getTileEntity(blockPos);

        if (tileEntity instanceof WeaponFurnaceTileEntity) {
            return (WeaponFurnaceTileEntity) tileEntity;
        }
        throw new IllegalStateException("invalid tile entity");
    }

    @Override
    public boolean canInteractWith(PlayerEntity playerIn) {
        return isWithinUsableDistance(ableToInteract, playerIn, BlockInit.WEAPON_FURNACE_BLOCK.get());
    }

    @Override
    public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {
        // 操作非GUI slot
        if (slotId > 10 && clickTypeIn != ClickType.QUICK_MOVE){
            return super.slotClick(slotId, dragType, clickTypeIn, player);
        }

        if (tileEntity.isWorking()){
            return ItemStack.EMPTY;
        }
        ItemStack stack = slotClickInnerFunc(slotId, dragType, clickTypeIn, player);
        // 检查是否可以working
        tileEntity.checkWorking();
        return stack;
    }

    private ItemStack slotClickInnerFunc(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {
        if (clickTypeIn == ClickType.PICKUP) {
            return pickup(slotId, dragType, clickTypeIn, player);
        } else if (clickTypeIn == ClickType.QUICK_MOVE) {
            return quickMove(slotId, player);
        } else if (clickTypeIn == ClickType.PICKUP_ALL) {
            return ItemStack.EMPTY;
        } else if (clickTypeIn == ClickType.QUICK_CRAFT) {
            return player.inventory.getItemStack();
        }

        return super.slotClick(slotId, dragType, clickTypeIn, player);
    }

    private ItemStack pickup(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {
        return super.slotClick(slotId, dragType, clickTypeIn, player);
    }

    private ItemStack quickMove(int slotId, PlayerEntity player) {
        return ItemStack.EMPTY;
    }

}
